<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>this事件</title>
    <script src="lib/react/react.development.js"></script>
    <script src="lib/react/react-dom.development.js"></script>
    <script src="lib/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      let message = "前端小溪";
      class HelloWorld extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            toggle: true,
          };
          // 在构造函数里通过bind重新绑定this
          this.toggleBtn = this.toggleBtn.bind(this);
        }
        toggleBtn() {
          console.log("我是前端小溪",this);
          // 注意: 这里必须返回一个state对象
          this.setState((state) => ({ toggle: !state.toggle }));
        }
        render() {
          return (
            <div>
              <h2 onClick={this.toggleBtn}>
                开关: {this.state.toggle ? "关闭" : "开启"}
              </h2>
            </div>
          );
        }
      }

      ReactDOM.render(<HelloWorld />, document.getElementById("root"));
    </script>
  </body>
</html>
